import Vue from "vue";
import {actions} from '../base';
import http from '@/common/http';
import helper from "@/common/helper";

import LocalConfigs from "@/导表/configs.json";
import LocalGames from "@/导表/games.json";
import LocalGamesHot from "@/导表/games-hot.json";
	
export default {
    namespaced: true,
    state: {
		channel: "",
		lists: [],
		listsHot: [],
		configs: {
			navbar: {},
			index: {
				bannerLeft: "",
				bannerRight: "",
			}
		},
		downloadRule: "",
    },
    mutations: {
		SET_CHANNEL(state, channel) {
			state.channel = channel;
			// if (channel) uni.setStorageSync("app.channel", channel);
		},
		SET_LISTS(state, lists) {
			state.lists = lists;
		},
		SET_LISTS_HOT(state, lists) {
			state.lists_hot = lists;
		},
		SET_CONFIGS(state, data) {
			state.configs = data;
			uni.setStorageSync("app.configs", data);
		},
		SET_DOWNLOAD_RULE(state, data) {
			state.downloadRule = data;
			uni.setStorageSync("app.downloadRule", data);
		},
    },
    actions: {
		GetConfigs({commit, getters}) {
			// if (helper.isDevelopment()) {
			// 	return {data: LocalConfigs};
			// }
			return new Promise((resolve, reject) => {
				http.get(`data/${getters.getChannel}/configs.json`).then(({data}) => {
					commit("SET_CONFIGS", data);
					return resolve(data);
				}).catch(e => {
					return reject(e);
				});
			});
		},
		GetDownloadRule({commit, getters}) {
			// if (helper.isDevelopment()) {
			// 	return {data: LocalConfigs};
			// }
			return new Promise((resolve, reject) => {
				http.get(`data/download-rule`).then(({data}) => {
					commit("SET_DOWNLOAD_RULE", data);
					return resolve(data);
				}).catch(e => {
					return reject(e);
				});
			});
		},
		GetGameList({commit, getters}) {
			// if (helper.isDevelopment()) {
			// 	return {data: LocalGames};
			// }
			return http.get(`data/${getters.getChannel}/games.json`);
		},
		GetGameListHot({commit, getters}) {
			// if (helper.isDevelopment()) {
			// 	return {data: LocalGamesHot};
			// }
			return http.get(`data/${getters.getChannel}/games-hot.json`);
		},
	},
	getters: {
		getChannel(state) {
			return state.channel || "88888";
		}
	}
};
